//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ParametricExercise.h"
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (boost::shared_ptr<QuantLib::ParametricExercise>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExercise = NULL;
#endif
	_ppParametricExercise = &childNative;
    
}
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (QuantLib::ParametricExercise& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phParametricExercise = NULL;
#endif
	_ppParametricExercise = new boost::shared_ptr<QuantLib::ParametricExercise> (&childNative);
    
    _ParametricExerciseOwner = owner;
    
}

Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (CParametricExercise^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppParametricExercise = new boost::shared_ptr<QuantLib::ParametricExercise> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExercise = NULL;
#endif
	if (!t->IsSubclassOf(CParametricExercise::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (QuantLib::Handle<QuantLib::ParametricExercise>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametricExercise = &childNative;
	_ppParametricExercise = &static_cast<boost::shared_ptr<QuantLib::ParametricExercise>>(childNative.currentLink());
    
    _ParametricExerciseOwner = owner;
}
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (QuantLib::Handle<QuantLib::ParametricExercise> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametricExercise = &childNative;
	_ppParametricExercise = &static_cast<boost::shared_ptr<QuantLib::ParametricExercise>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Montecarlo::CParametricExercise::CParametricExercise (QuantLib::ParametricExercise childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExercise = NULL;
#endif
	_ppParametricExercise = new boost::shared_ptr<QuantLib::ParametricExercise> (new QuantLib::ParametricExercise (childNative));
    
}
#endif

Cephei::QL::Methods::Montecarlo::CParametricExercise::~CParametricExercise ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppParametricExercise != NULL)
    {
	    delete _ppParametricExercise;
        _ppParametricExercise = NULL;
    }
}
Cephei::QL::Methods::Montecarlo::CParametricExercise::!CParametricExercise ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppParametricExercise != NULL)
    {
	    delete _ppParametricExercise;
    }
}
QuantLib::ParametricExercise& Cephei::QL::Methods::Montecarlo::CParametricExercise::GetReference ()
{
    if (_ppParametricExercise == NULL) throw gcnew NativeNullException ();
	return **_ppParametricExercise;
}
boost::shared_ptr<QuantLib::ParametricExercise>& Cephei::QL::Methods::Montecarlo::CParametricExercise::GetShared ()
{
    if (_ppParametricExercise == NULL) throw gcnew NativeNullException ();
	return *_ppParametricExercise;
}
QuantLib::ParametricExercise* Cephei::QL::Methods::Montecarlo::CParametricExercise::GetPointer ()
{
    if (_ppParametricExercise == NULL) throw gcnew NativeNullException ();
	return &**_ppParametricExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ParametricExercise>& Cephei::QL::Methods::Montecarlo::CParametricExercise::GetHandle ()
{
	if (_phParametricExercise == NULL)
	{
		_phParametricExercise = new Handle<QuantLib::ParametricExercise> (*_ppParametricExercise);
	}
	return *_phParametricExercise;
}
#endif
bool Cephei::QL::Methods::Montecarlo::CParametricExercise::HasNative () 
{
	return (_ppParametricExercise != NULL);
}

Boolean Cephei::QL::Methods::Montecarlo::CParametricExercise::Exercise (UInt64 exerciseNumber, Cephei::IVector<Double>^ parameters, Cephei::IVector<Double>^ variables)
{
    CoVector<Double>^ _Cparameters;
    CoVector<Double>^ _Cvariables;
    try
    {
        QuantLib::Size _exerciseNumber = (QuantLib::Size)ValueHelper::Convert (exerciseNumber);
        _Cparameters = safe_cast<CoVector<Double>^> (parameters);
        _Cparameters->Lock();
        INativeVector<Double>^ _NCIparameters = _Cparameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCparameters = safe_cast<CDoubleVector^>(_NCIparameters);
        std::vector<QuantLib::Real>& _parameters = static_cast<std::vector<QuantLib::Real>&> (_NCparameters->GetReference ());
        _Cvariables = safe_cast<CoVector<Double>^> (variables);
        _Cvariables->Lock();
        INativeVector<Double>^ _NCIvariables = _Cvariables->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvariables = safe_cast<CDoubleVector^>(_NCIvariables);
        std::vector<QuantLib::Real>& _variables = static_cast<std::vector<QuantLib::Real>&> (_NCvariables->GetReference ());
    	bool _rv = (bool)(*_ppParametricExercise)->exercise ( _exerciseNumber,  _parameters,  _variables );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cparameters != nullptr) _Cparameters->Unlock();
        if (_Cvariables != nullptr) _Cvariables->Unlock();
    }
}
Cephei::QL::Methods::Montecarlo::IParametricExercise^ Cephei::QL::Methods::Montecarlo::CParametricExercise::Guess (UInt64 exerciseNumber, Cephei::IVector<Double>^ parameters)
{
    CoVector<Double>^ _Cparameters;
    try
    {
        QuantLib::Size _exerciseNumber = (QuantLib::Size)ValueHelper::Convert (exerciseNumber);
        _Cparameters = safe_cast<CoVector<Double>^> (parameters);
        _Cparameters->Lock();
        INativeVector<Double>^ _NCIparameters = _Cparameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCparameters = safe_cast<CDoubleVector^>(_NCIparameters);
        std::vector<QuantLib::Real>& _parameters = static_cast<std::vector<QuantLib::Real>&> (_NCparameters->GetReference ());
    	(*_ppParametricExercise)->guess ( _exerciseNumber,  _parameters );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cparameters != nullptr) _Cparameters->Unlock();
    }
}
Cephei::IVector<UInt64>^ Cephei::QL::Methods::Montecarlo::CParametricExercise::NumberOfParameters::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppParametricExercise)->numberOfParameters ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<UInt64>^ Cephei::QL::Methods::Montecarlo::CParametricExercise::NumberOfVariables::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppParametricExercise)->numberOfVariables ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

